package com.flower.web.controller.business;

import cn.hutool.core.lang.Snowflake;
import com.flower.common.annotation.Log;
import com.flower.common.core.controller.BaseController;
import com.flower.common.core.domain.AjaxResult;
import com.flower.common.core.page.TableDataInfo;
import com.flower.common.enums.BusinessType;
import com.flower.common.utils.poi.ExcelUtil;
import com.flower.system.domain.BizForumReply;
import com.flower.system.service.IBizForumReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 论坛回复Controller
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/system/reply")
public class BizForumReplyController extends BaseController
{
    @Autowired
    private IBizForumReplyService bizForumReplyService;

    /**
     * 查询论坛回复列表
     */
    @PreAuthorize("@ss.hasPermi('system:reply:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizForumReply bizForumReply)
    {
        startPage();
        List<BizForumReply> list = bizForumReplyService.selectBizForumReplyList(bizForumReply);
        return getDataTable(list);
    }

    /**
     * 导出论坛回复列表
     */
    @PreAuthorize("@ss.hasPermi('system:reply:export')")
    @Log(title = "论坛回复", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizForumReply bizForumReply)
    {
        List<BizForumReply> list = bizForumReplyService.selectBizForumReplyList(bizForumReply);
        ExcelUtil<BizForumReply> util = new ExcelUtil<BizForumReply>(BizForumReply.class);
        util.exportExcel(response, list, "论坛回复数据");
    }

    /**
     * 获取论坛回复详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:reply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bizForumReplyService.selectBizForumReplyById(id));
    }

    /**
     * 新增论坛回复
     */
    @PreAuthorize("@ss.hasPermi('system:reply:add')")
    @Log(title = "论坛回复", businessType = BusinessType.INSERT)
    @PostMapping({"/add",""})
    public AjaxResult add(@RequestBody BizForumReply bizForumReply)
    {
        //添加雪花id
        bizForumReply.setId(new Snowflake(1,1).nextIdStr());
        //根据token获取登录用户
        String userId = getLoginUser().getUser().getUserId().toString();
        String userName = getLoginUser().getUser().getUserName();

        bizForumReply.setUserId(userId);
        bizForumReply.setNickName(userName);
        return toAjax(bizForumReplyService.insertBizForumReply(bizForumReply));
    }

    /**
     * 修改论坛回复
     */
    @PreAuthorize("@ss.hasPermi('system:reply:edit')")
    @Log(title = "论坛回复", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody BizForumReply bizForumReply)
    {
        return toAjax(bizForumReplyService.updateBizForumReply(bizForumReply));
    }

    /**
     * 删除论坛回复
     */
    @PreAuthorize("@ss.hasPermi('system:reply:remove')")
    @Log(title = "论坛回复", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizForumReplyService.deleteBizForumReplyByIds(ids));
    }
}
